<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header class="home_header">
      <div>
        <img src="../assets/logo.png" alt="">
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体内容区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isToggle? '64px':'200px'" class="home_aside">
        <div class="meunToggle" @click="isMeunToggle"><i :class="isToggle? 'el-icon-s-unfold':'el-icon-s-fold'"></i></div>
        <!-- 侧边栏导航菜单 -->
        <el-menu
          :unique-opened="true"
          background-color="#607d8b"
          text-color="#fff"
          active-text-color="#00bcd4"
          :collapse="isToggle"
          :collapse-transition="false"
          :router="true"
          :default-active="menuItemPath">
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item @click="astivMenuItem('/' + subitem.path)" :index="'/' + subitem.path + ''" v-for="subitem in item.children" :key="subitem.id">
              <i class="el-icon-menu"></i>
              <span>{{ subitem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main class="home_main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  data() {
    return {
      // 左侧的菜单数据
      menuList: [],
      // 图标
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否把侧边栏折叠
      isToggle: false,
      // 菜单高亮
      menuItemPath: ''
    }
  },
  created() {
    this.getLeftList()
    this.menuItemPath = window.sessionStorage.getItem('activName')
  },
  methods: {
    // 退出按钮
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单数据
    async getLeftList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 折叠菜单
    isMeunToggle() {
      this.isToggle = !this.isToggle
    },
    // 激活菜单选项
    astivMenuItem(activName) {
      window.sessionStorage.setItem('activName', activName)
      this.menuItemPath = activName
    }
  }
}
</script>

<style lang="less" scoped>
.el-container{
  height: 100%;
}
.home_header{
  background-color: #455a64;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  align-items: center;
  > div{
    display: flex;
    align-items: center;
      img{
        height: 50px;
      }
      span{
        margin-left: 15px;
      }
  }
}
.home_aside{
  background-color: #607d8b;
  .el-menu{
    border-right: none;
  }
}
.home_main{
  background-color: #FFFFFF;
}
.iconfont{
  margin-right: 10px;
}
.meunToggle{
  width: 100%;
  height: 40px;
  font-size: 30px;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
  color: #ccc;
}
</style>
